package top.lrshuai.websocket.controller;

import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lrshuai.websocket.user.service.IUserService;
import top.lrshuai.websocket.ws.entity.WebSocketDTO;
import top.lrshuai.websocket.ws.utils.ChannelGroupUtil;
import top.lrshuai.websocket.ws.utils.MessageSender;

import java.util.concurrent.ConcurrentHashMap;

@RestController
public class IndexController {

    @Autowired
    private MessageSender messageSender;

    @Autowired
    private IUserService userService;

    @GetMapping("/online")
    public Object online(){
        ConcurrentHashMap<String, Channel> userChannelMap = ChannelGroupUtil.getInstance().getUserChannelMap();
        return userChannelMap.size();
    }

    @GetMapping("/send")
    public Object send(String msg,String module){
        messageSender.sendAllMessage(WebSocketDTO.data(msg,module));
        return "success";
    }

    @GetMapping("/test")
    public Object test(){
        userService.testTrans();
        return "success";
    }
}
